import {_decorator, Component, Label, Node} from 'cc';
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {PriorityEnum} from "db://assets/scripts/game/PriorityEnum";
import {BaseEnum} from "db://assets/scripts/game/BaseEnum";
import {Utils} from "db://assets/scripts/utils/Utils";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {CSVManager} from "db://assets/scripts/common/CSVManager";
import {UIManager} from "db://assets/scripts/common/UIManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {EventType} from "db://assets/scripts/common/EventType";
import {EventManager} from "db://assets/scripts/common/EventManager";
import {BossBloodBarController} from "db://assets/scripts/game/BossBloodBarController";
import {GameState} from "db://assets/scripts/game/GameState";

const {ccclass, property} = _decorator;

/**
 * 战斗界面脚本
 */
@ccclass('FightPanelController')
export class FightPanelController extends Component {
    /**
     * 手柄节点
     */
    @property(Node)
    public joystickNode: Node;
    /**
     * 金币数量
     */
    @property(Label)
    public goldLabel: Label;
    /**
     * 等级
     */
    @property(Label)
    public levelLabel: Label;
    /**
     * boss血量进度条节点
     */
    @property(Node)
    public bossBloodBarNode: Node;

    /**
     * 调试点击次数
     */
    private debugClickCount: number = 0;

    onEnable() {
        EventManager.on(EventType.REFRESH_GOLD, this.refreshGold, this);
        EventManager.on(EventType.REFRESH_FLOOR, this.refreshLevel, this);
    }

    onDisable() {
        EventManager.off(EventType.REFRESH_GOLD, this.refreshGold, this);
        EventManager.off(EventType.REFRESH_FLOOR, this.refreshLevel, this);
    }

    /**
     * 展示战斗界面
     */
    public show() {
        this.bossBloodBarNode.active = false;

        this.refreshGold();
        this.refreshLevel();
        
        if (GameManager.getInstance().gameBossProxy.getBossNode()) {
            let bossInfoRow = CSVManager.getInstance().getRowDatas("base", "ID", BaseEnum.BOSS_01);
            let script = this.bossBloodBarNode.getComponent(BossBloodBarController);
            // 血量在第7列，未来写成枚举
            script.show(GameManager.getInstance().gameBossProxy.getBossController(), Number(bossInfoRow[7]));
            // script.show(GameManager.getInstance().gameBossProxy.getBossController(), Number(bossInfo.hp));
        }

        this.debugClickCount = 0;
    }

    /**
     * 刷新金币
     */
    private refreshGold() {
        this.goldLabel.string = Utils.formatMoney(PlayerDataManager.getInstance().getPlayer().gold);
    }

    /**
     * 刷新等级
     */
    private refreshLevel() {
        this.levelLabel.string = `第${PlayerDataManager.getInstance().getPlayer().level}层`;
    }

    /**
     * 暂停
     */
    public pause() {
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        UIManager.getInstance().showDialog("pause/pausePanel", null, null, PriorityEnum.DIALOG);
        GameManager.getInstance().gameStateProxy.setGameState(GameState.PAUSE);
    }

    /**
     * 调试
     */
    public debug() {
        AudioManager.getInstance().playSound(AudioPaths.clickSound);

        this.debugClickCount += 1;

        if (this.debugClickCount >= 1) {
            this.debugClickCount = 0;
            UIManager.getInstance().showDialog("debug/debugPanel", null, null, PriorityEnum.DIALOG);
        }
    }
}

